package com.liujianlei.forum_system.controller;

import com.liujianlei.forum_system.common.AppResult;
import com.liujianlei.forum_system.common.ResultCode;
import com.liujianlei.forum_system.config.AppConfig;
import com.liujianlei.forum_system.exception.ApplicationException;
import com.liujianlei.forum_system.model.Article;
import com.liujianlei.forum_system.model.ArticleReply;
import com.liujianlei.forum_system.model.User;
import com.liujianlei.forum_system.service.IArticleReplyService;
import com.liujianlei.forum_system.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author tongchen
 * @create 2023-06-15 21:56
 */
@RequestMapping("/reply")
@RestController
@Slf4j
@Api(tags = "帖子回复的接口")
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IArticleReplyService articleReplyService;
    @PostMapping("/create")
    @ApiOperation(("新增评论的接口"))
    public AppResult create(HttpServletRequest request, @RequestParam@NonNull@ApiParam(value = "帖子id") Long id ,@RequestParam@NonNull@ApiParam(value = "评论内容") String content){
        //校验用户登录信息
        //用户是否登录
        HttpSession session = request.getSession(false);
        if(session==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        User loginUser = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(loginUser==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        //校验用户状态
        if(loginUser.getState()==1){
            log.error("用户状态为禁言状态");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //查阅id对应的文章信息

        //封装对象
        Article article = articleService.selectDetailById(id);
        if(article==null){
            log.error("该id对应的帖子为空，不符合要求");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        //查看文章对应的信息
        if(article.getDeleteState()==1||article.getState()==1){
            log.error("帖子状态不正常");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setContent(content);
        articleReply.setArticleId(article.getId());
        articleReply.setPostUserId(loginUser.getId());
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReplyService.create(articleReply);
        log.info("评论信息新增成功");
        return AppResult.success("文章回复新增成功");
    }
    @ApiOperation("查看帖子的方法")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getReply(@NonNull@RequestParam()@ApiParam("文章id") Long articleId){
        //直接调用service显示数据
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success("回复显示成功",articleReplies);
    }
}
